﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LevelEditor.model.core;

namespace LevelEditor.model.elements
{
    class PGameObjectsFactory
    {
        #region Variables
        private static int m_pGameObjectsCreatedNb = 0;

        #endregion

        #region Constructeur prive - Pas d'instanciation
        private PGameObjectsFactory() { }
        #endregion

        #region Methodes statiques
        // Construction d'objets from view
        public static AbstractPGameObject createPGameObject(int key)
        {
            AbstractPGameObject pGameObject = null;
            Dictionary<String, String> pGameObjectDefinition = Library.getPGameObjectDefinition(key);
            String name = null;

            switch (pGameObjectDefinition[Library.TYPE_KEY])
            {
                case Library.BARREL_TYPE:
                    name = Library.BARREL_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Barrel(name, pGameObjectDefinition, 1);
                    break;

                case Library.CRATE_TYPE:
                    name = Library.CRATE_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Crate(name, pGameObjectDefinition, 1);
                    break;

                case Library.CHARACTER_TYPE:
                    name = Library.CHARACTER_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Character(name, pGameObjectDefinition);
                    break;

                default:
                    return null;
            }

            m_pGameObjectsCreatedNb++;

            return pGameObject;
        }

        //Construction d'un objet from xml
        private static AbstractPGameObject createPGameObject(Dictionary<String, String> pGameObjectData)
        {
            AbstractPGameObject pGameObject = null;
            String name = null;

            switch (pGameObjectData[Library.TYPE_KEY])
            {
                case Library.BARREL_TYPE:
                    name = Library.BARREL_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Barrel(name, pGameObjectData, 1);
                    break;

                case Library.CRATE_TYPE:
                    name = Library.CRATE_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Crate(name, pGameObjectData, 1);
                    break;

                case Library.CHARACTER_TYPE:
                    name = Library.CHARACTER_TYPE + "_" + m_pGameObjectsCreatedNb;
                    pGameObject = new Character(name, pGameObjectData);
                    break;

                default:
                    return null;
            }

            return pGameObject;
        }

        //Construction d'une liste d'objets en fonction d'une liste de donnees from xml
        public static List<AbstractPGameObject> createPGameObjectsList(List<Dictionary<String, String>> pGameObjectsDataList)
        {
            List<AbstractPGameObject> pGameObjects = new List<AbstractPGameObject>();

            for (int i = 0; i < pGameObjectsDataList.Count; ++i)
            {
                AbstractPGameObject pGameObject = createPGameObject(pGameObjectsDataList[i]);

                if (pGameObject != null)
                {
                    pGameObjects.Add(pGameObject);
                }
            }

            return pGameObjects;
        }

        public static void resetPGameObjectsCreatedNb()
        {
            m_pGameObjectsCreatedNb = 0;
        }

        #endregion
    }
}
